package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;

import edu.pku.notation.runtime.constance.ColorConstance;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.ObjectFlow;
import edu.pku.sei.transformation.editor.views.figures.ConnectionFigure;

public class ObjectFlowEditpart extends TransformationLinkEditPart {

	public ObjectFlowEditpart(MetaShapeElement conn) {
		super(conn);
	}

	@Override
	protected IFigure createFigure() {
		ConnectionFigure figure = new ConnectionFigure();
		figure.setLabel("<<object flow>>",
				ConnectionFigure.CONNECTION_FIGURE_MIDDLE);
		figure.setTargetDecoration(ConnectionFigure.ARROW);
		return figure;
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.RELATIONSHIP_SOURCE.equals(propertyName) 
				|| ModelPackage.RELATIONSHIP_TARGET.equals(propertyName)) {
			refreshVisuals();
		} else {
			super.handlePropertyChanged(event);
		}
	}
	
	@Override
	protected void refreshVisuals() {
		ObjectFlow flow = (ObjectFlow) ((MetaLink)getModel()).getModelElement();
		if (!flow.checkType()) {
			figure.setForegroundColor(ColorConstance.RED);
		} else {
			figure.setForegroundColor(ColorConstance.BLACK);
		}
		super.refreshVisuals();
	}

}
